import service from './index.js';

export const getCaptcha = async () => {
  try {
    const res = await service.get("jestic-user-center/v1/pb/user-center/basic/member/getCaptcha");
    if (res && res.validCodeBase64 && res.validCodeReqNo) {
      return {
        image: `data:image/png;base64,${res.validCodeBase64}`,
        reqNo: res.validCodeReqNo
      };
    }
    throw new Error('无效的验证码响应');
  } catch (error) {
    throw error;
  }
};

export const login = async (values) => {
  try {
    const md5 = await import('md5').then(m => m.default);
    const loginData = {
      account: values.username,
      password: md5(values.password),
      validCodeReqNo: values.validCodeReqNo,
      validCode: values.captcha
    };
    const res = await service.post('jestic-account-admin/v1/pb/account-admin/system/user/login', loginData);
    return res;
  } catch (error) {
    throw error;
  }
};

export const getMenuTree = async () => {
  try {
    const res = await service.get('jestic-account-admin/v1/pt/account-admin/system/user/getRouter');
    return res;
  } catch (error) {
    throw error;
  }
};

export const getLoginUser = async () => {
  try {
    const res = await service.get('jestic-account-admin/v1/pt/account-admin/system/user/getLoginUser');
    return res;
  } catch (error) {
    throw error;
  }
};

export const logout = async () => {
  try {
    const res = await service.post('jestic-account-admin/v1/pt/account-admin/system/user/logout');
    return res;
  } catch (error) {
    throw error;
  }
};